package com.cj.bus.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cj.bus.common.utils.RequestUtils;
import com.cj.bus.entity.BusResourceCommentEntity;
import com.cj.bus.entity.BusResourceEntity;
import com.cj.bus.entity.BusUserBrowseEntity;
import com.cj.bus.entity.BusUserCollectEntity;
import com.cj.bus.mapper.BusResourceMapper;
import com.cj.bus.service.BusResourceCommentService;
import com.cj.bus.service.BusResourceService;
import com.cj.bus.service.BusUserBrowseService;
import com.cj.bus.service.BusUserCollectService;

/**
 * <p>
 * 资源信息表 服务实现类
 * </p>
 *
 * @author yangh73
 * @since 2021-01-12
 */
@Service
public class IBusResourceServiceImpl extends ServiceImpl<BusResourceMapper, BusResourceEntity> implements BusResourceService {

	@Resource
	private BusUserBrowseService busUserBrowseService;
	
	@Resource
	private BusUserCollectService busUserCollectService;
	
	@Resource
	private BusResourceCommentService busResourceCommentService;
	
	@Override
	public List<BusResourceEntity> selectList(String searchKey, List<Integer> types, String status) {
		return this.baseMapper.selectList(searchKey, types, status);
	}

	@Override
	@Transactional
	public void store(BusResourceEntity resourceEntity) {
		Long loginId = RequestUtils.getLoginId();
		if(null != loginId) {
			if(resourceEntity.getPublishUserId().intValue() != loginId.intValue()) {
				int exists = busUserCollectService.count(
						new QueryWrapper<BusUserCollectEntity>()
						.eq("collect_type", "02")
						.eq("collect_user_id", loginId)
						.eq("collect_id", resourceEntity.getResourceId())
				 );
				if(exists == 0) {
					//插入记录
					this.baseMapper.addStoreNums(resourceEntity.getResourceId());
				}
				BusUserCollectEntity record = new BusUserCollectEntity();
				record.setCollectType("02");
				record.setCollectUserId(loginId);
				record.setCollectId(resourceEntity.getResourceId());
				record.setCollectUserName(RequestUtils.getLoginName());
				record.setCreateTime(LocalDateTime.now());
				busUserCollectService.saveOrUpdate(record);
			}
			
		}
		
	}

	@Override
	@Transactional
	@Async
	public void browse(BusResourceEntity resourceEntity) {
		//判断是否登录
		Long loginId = RequestUtils.getLoginId();
		if(null != loginId) {
			if(resourceEntity.getPublishUserId().intValue() != loginId.intValue()) {
				//如果登录 则判断 登录ID与资源ID是否已经关联了 关联了就不增加 浏览记录 否则 增加浏览记录
				int exists = busUserBrowseService.count(
						new QueryWrapper<BusUserBrowseEntity>()
						.eq("browse_type", "02")
						.eq("browse_user_id", loginId)
						.eq("browse_id", resourceEntity.getResourceId())
						);
				//插入
				BusUserBrowseEntity userBrowseEntity = new BusUserBrowseEntity();
				userBrowseEntity.setBrowseType("02");
				userBrowseEntity.setBrowseId(resourceEntity.getResourceId());
				userBrowseEntity.setBrowseUserId(loginId);
				userBrowseEntity.setBrowseUserName(RequestUtils.getLoginName());
				userBrowseEntity.setCreateTime(LocalDateTime.now());
				busUserBrowseService.saveOrUpdate(userBrowseEntity);
			}
		}
		//资源浏览次数+1
		this.baseMapper.addBrowseNums(resourceEntity.getResourceId());
	}

	@Override
	@Transactional
	public void comment(BusResourceCommentEntity busResourceCommentEntity) {
		//
		if(busResourceCommentEntity.getUserId() == null) {
			busResourceCommentEntity.setUserId(RequestUtils.getLoginId());
			busResourceCommentEntity.setUserName(RequestUtils.getLoginName());
		}
		busResourceCommentEntity.setCreateTime(LocalDateTime.now());
		busResourceCommentService.save(busResourceCommentEntity);
		//评论数+1
		this.baseMapper.addCommentNums(busResourceCommentEntity.getResourceId());
	}

}
